package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.*;
import com.itheima.mapper.*;
import com.itheima.service.db.SoulTestService;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.signature.qual.PolySignature;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SoulTestServiceImpl implements SoulTestService {

    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Override
    public List<Questionnaire> findAllQuestionnaire() {
        List<Questionnaire> questionnaireList = questionnaireMapper.selectList(new QueryWrapper<>());
        return questionnaireList;
    }

    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    //根据调查问卷的id  查询所有所属的问题
    @Override
    public List<SoulQuestion> findByQuestionnaireId(Integer questionnaireId) {
        //构建条件
        QueryWrapper<SoulQuestion> qw = new QueryWrapper<>();
        qw.eq("questionnaire_id", questionnaireId);
        List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(qw);
        return soulQuestionList;

    }

    //根据灵魂问题id 查出所有的选项
    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;
    @Override
    public List<SoulQuestionOption> findBySoulQuestionId(Integer soulQuestionId) {
        //构建条件
        QueryWrapper<SoulQuestionOption> qw = new QueryWrapper<>();
        qw.eq("question_id", soulQuestionId);
        List<SoulQuestionOption> soulQuestionOptionList = soulQuestionOptionMapper.selectList(qw);
        return soulQuestionOptionList;

    }



    @Override
    public SoulQuestionOption findBySoulQuestionIdAndOptionId(Integer optionId) {
        //构建条件
        QueryWrapper<SoulQuestionOption> qw = new QueryWrapper<>();
//        qw.eq("question_id", soulQuestionId);
        qw.eq("id", optionId);
        SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(qw);
        return soulQuestionOption;
    }

    @Autowired
    private QuestionnaireResultMapper questionnaireResultMapper;
    @Override
    public QuestionnaireResult findByScoreAndQuestionnaireId(Integer questionnaireId, Integer score) {

        //构建条件
        QueryWrapper<QuestionnaireResult> qw = new QueryWrapper<>();
        qw.eq("questionnaire_id", questionnaireId);

        //初始化范围
        String scoreStr = null;
        if (score <= 20) {
            scoreStr = "0,20";
        }
        if (score > 20 && score <= 40) {
            scoreStr = "21,40";
        }
        if (score > 40 && score <= 55) {
            scoreStr = "41,55";
        }
        if (score > 55 && score <= 100) {
            scoreStr = "56,100";
        }
        qw.eq("scope", scoreStr);

        QuestionnaireResult questionnaireResult = questionnaireResultMapper.selectOne(qw);
        return questionnaireResult;
    }

    @Autowired
    private QuestionnaireReportMapper questionnaireReportMapper;
    @Override
    public Integer saveReport(QuestionnaireReport report) {
        questionnaireReportMapper.insert(report);
        Integer id = report.getId();
        return id;

    }

    @Override
    public QuestionnaireReport findById(Integer id) {
        return questionnaireReportMapper.selectById(id);
    }

    @Override
    public List<QuestionnaireReport> findByconclusionId(Integer id) {
        //构建条件
        QueryWrapper<QuestionnaireReport> qw = new QueryWrapper<>();
        qw.eq("conclusion_id", id);
        return questionnaireReportMapper.selectList(qw);
    }


    @Override
    public QuestionnaireResult findResultById(Integer id) {
       return questionnaireResultMapper.selectById(id);
    }


    @Override
    public SoulQuestion findByQuestionId(Integer id) {
        return soulQuestionMapper.selectById(id);
    }

    @Override
    public QuestionnaireReport findReportByUserId(Long userId,Integer conclusionId) {
        //构建条件
        QueryWrapper<QuestionnaireReport> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("conclusion_id", conclusionId);

        QuestionnaireReport reportByuserId = questionnaireReportMapper.selectOne(qw);
        return reportByuserId;
    }

    @Override
    public QuestionnaireReport findReportByUserId2(Long userId) {
        QueryWrapper<QuestionnaireReport> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.orderByDesc("created");
        List<QuestionnaireReport> reportList = questionnaireReportMapper.selectList(qw);
        if (reportList.size() != 0) {

            return reportList.get(0);
        } else {
            return null;
        }


    }

    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;
    @Override
    public void saveQuestionUserLock(QuestionUserLock questionUserLock) {

        questionUserLockMapper.insert(questionUserLock);


    }

    @Override
    public QuestionUserLock findByUserId(Long userId,Integer questionnaireId) {
        QueryWrapper<QuestionUserLock> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("questionnaire_id", questionnaireId);

        return questionUserLockMapper.selectOne(qw);
    }

    @Override
    public void updateUserLock(QuestionUserLock userLock) {
        questionUserLockMapper.updateById(userLock);
    }


}
